import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { useState } from 'react';
import useFetch from '../useFetch';
export default (function (_ref) {
  var format = _ref.format,
    transform = _ref.transform,
    services = _ref.services;
  var _useState = useState(),
    _useState2 = _slicedToArray(_useState, 2),
    entity = _useState2[0],
    setEntity = _useState2[1];
  var handleGet = useFetch(function (params) {
    if (services.get) {
      return services.get(params).then(function (res) {
        if (res && res.success) {
          var currentEntity = format ? format(res.data) : res.data;
          setEntity(currentEntity);
          res.data = currentEntity;
        }
        return res;
      });
    }
    return Promise.reject();
  }, {
    manual: true
  });
  var handleCreate = useFetch(function (params) {
    if (services.create) {
      return services.create(transform ? transform(params) : params);
    }
    return Promise.reject();
  }, {
    manual: true
  });
  var handleUpdate = useFetch(function (params) {
    if (services.update) {
      return services.update(transform ? transform(params) : params);
    }
    return Promise.reject();
  }, {
    manual: true
  });
  var handleDelete = useFetch(function (params) {
    if (services.del) {
      return services.del(params);
    }
    return Promise.reject();
  }, {
    manual: true
  });
  return {
    entity: entity,
    setEntity: setEntity,
    handleGet: services.get ? handleGet : undefined,
    handleCreate: services.create ? handleCreate : undefined,
    handleUpdate: services.update ? handleUpdate : undefined,
    handleDelete: services.del ? handleDelete : undefined
  };
});